<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Parse image url</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        body{
            width: 100%;
            overflow-y: scroll;
            overflow-x: auto;
            overflow-y: hidden;
            zoom: 1;
            left: 0;
            top: 0;
            background: url('images/bg.jpeg') no-repeat;
            background-size: 100% 100%;
            background-position: center;
            background-size: cover;
            background-attachment: fixed;
        }
        h1{
            margin: 20px;
            padding: 20px;
            color: #35241b;
            text-align: center; 
        }
        .main{
            width:600px;
            height: 400px;
            margin: 50px auto;
            padding: 30px;
            /* background: #fff;
            opacity: 0.9; */
        }
        input,button:focus {
            outline: none;
            border: 0;
        }
        #inp{
            width: 450px;
            height: 35px;
            padding: 5px 10px;
            border: 0;
            border-top-left-radius: 8px;
            border-bottom-left-radius: 8px;
            background: #fff;
            font-size: 20px;
        }
        img{
            /* border: #ddd solid 1px; */
        }
        button{
            height: 47px;
            width: 100px;
            line-height: 35px;
            text-align: center;
            border: 0;
            border-top-right-radius: 8px;
            border-bottom-right-radius: 8px;
            font-size: 20px;
            margin-left: -8px;
            color: #fff;
            background: linear-gradient(#bcbcbc,#aeaeae);
            cursor: pointer;
        }
        .boxImg{
            display: none;
        }
        button img{
            width: 20px;
            line-height: 47px;
        }
    </style>
</head>
<body>
    <h1>书山有路勤为径,学海无涯苦做舟。</h1>
    <div class="main">
        <input onchange="inpChange(this.id)" id="inp" type="text">
        <button id="btn" title="解析">
            <img src="images/icon.png" alt="">
        </button>
        <div class="boxImg">
            <img id="img" src="" alt="解析失败">
        </div>
    </div>
    <script>
        var inp= document.getElementById('inp'),
            img= document.getElementById('img'),
            btn= document.getElementById('btn'),
            iconImg= btn.firstElementChild,
            boxImg= document.querySelector('.boxImg');

        function inpChange(inp){
            var inp= document.getElementById('inp');
            var inpText= 'http:'+inp.value;
            img.src= inpText;
        }
        btn.onclick = function (){
            console.log(img.width);
            if(img.width > 100){
                boxImg.style.display="block";
            }else{
                alert('解析失败')
            }
        }
        btn.onmouseover= function (){
            iconImg.src= "images/icon_hover.png";
        }
        btn.onmouseout= function (){
            iconImg.src= "images/icon.png"
        }



        // // 下载
        // function downloadIamge(selector, name) {
        //     // 通过选择器获取img元素
        //     var img = document.querySelector(selector)
        //     // 将图片的src属性作为URL地址
        //     var url = img.src
        //     var a = document.createElement('a')
        //     var event = new MouseEvent('click')
            
        //     a.download = name || '下载图片名称'
        //     a.href = url
            
        //     a.dispatchEvent(event)
        // }

        // // 调用方式
        // // 参数一： 选择器，代表img标签
        // // 参数二： 图片名称，可选
        // downloadIamge(iconImg, '图片名称')
    </script>
</body>
</html>